﻿using System.Diagnostics;
using System.Windows;
using RVM.Infrastructure.Communication;
using RVM.Infrastructure.Communication.WCF.NetPipe;
using RVM.Infrastructure.Identification.Process;
using RvmShared;


namespace RvmExporter
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        #region Private Members

        /// <summary>
        /// Sample ViewModel which is exported.
        /// </summary>
        private ViewModelParent _viewModelToShare = new ViewModelParent();

        /// <summary>
        /// Exporter, exports sample ViewModel.
        /// </summary>
        private IViewModelExporter _exporter = new NetPipeViewModelExporter(
            new ProcessIdentificationProvider(SimpleSession.Current, SimpleSession.ProcessName), 
            SimpleSession.Current);
        
        #endregion

        #region Constructors

        /// <summary>
        /// Default constructor.
        /// Initializes application.
        /// </summary>
        public MainWindow()
        {
            _viewModelToShare.ExportViewModel("EntryPoint");
            _viewModelToShare.Simple = new SimpleViewModel();
            _viewModelToShare.Traditional = new TraditionalViewModel();

            DataContext = _viewModelToShare;
            InitializeComponent();
            Title += " (" + Process.GetCurrentProcess().Id + ")";
            InfoText.Text += " in session " + SimpleSession.Current.Id;
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Handler when user clicks Share button.
        /// Starts the actual exporting.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnShareClick(object sender, RoutedEventArgs e)
        {
            if (_exporter.Connected)
            {
                _exporter.Stop();
                ShareButton.Content = "Share";
            }
            else
            {
                _exporter.Start();
                ShareButton.Content = "Stop Sharing";
            }
        }

        /// <summary>
        /// Handler when user closes the application.
        /// Ensures that the export is stopped.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnWindowClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if(_exporter.Connected)
                _exporter.Stop();
        }

        #endregion
    }
}
